#include "Visualizer.h"

#include <maya\MString.h>
#include <maya\MGlobal.h>

void Visualizer::drawCube(double x, double y, double z, double width, double height, double depth)
{
  // clear selection
  MGlobal::executeCommand("select -cl", true, true);

  // create the box of size width, height, depth
  MString msizex;
  msizex.set(width);
  MString msizey;
  msizey.set(height);
  MString msizez;
  msizez.set(depth);
  MString si;
  MString sj;
  MString sk;
  MString cmd;
	MString cmd1("polyCube -ch on -o on -w ^1s -h ^2s -d ^3s -cuv 4");
	cmd.format(cmd1, msizex, msizey, msizez);
  MGlobal::executeCommand(cmd, true, true);

  // move it into place (x,y,z)
  MString tx;
  tx.set(x);
  MString ty;
  ty.set(y);
  MString tz;
  tz.set(z);
  MString cmd2("move -r ^1s ^2s ^3s");
  cmd.format(cmd2, tx, ty, tz);
  MGlobal::executeCommand(cmd, true, true);
}

void Visualizer::drawSpace(int voxelsPerDimetion, const MBoundingBox& bb){


	for (int i = 0; i < voxelsPerDimetion; ++i)
		for (int j = 0; j < voxelsPerDimetion; ++j)
			for (int k = 0; k < voxelsPerDimetion; ++k){
				drawVoxel(voxelsPerDimetion, bb, i,j,k);
			}

}

void Visualizer::drawRay(const MPoint& origin, const MVector& direction){
	drawRay(origin, direction, 30);
}

void Visualizer::drawRay(const MPoint& origin, const MPoint& dest){
	MVector dir = dest - origin;
	double length = dir.length();
	dir.normalize();
	drawRay(origin, dir, length);
}

void Visualizer::drawRay(const MPoint& origin, const MVector& direction, double length){
	MString cmd1("curve -d 1 -p ^1s ^2s ^3s -p ^4s ^5s ^6s");
	MString sx,sy,sz,ex,ey,ez;
	sx.set(origin.x);
	sy.set(origin.y);
	sz.set(origin.z);
	ex.set(origin.x + length*direction.x);
	ey.set(origin.y + length*direction.y);
	ez.set(origin.z + length*direction.z);
	MString cmd;
	cmd.format(cmd1, sx,sy,sz,ex,ey,ez);
	MGlobal::executeCommand(cmd, true, true);
	MGlobal::executeCommand("select -cl", true, true);
}

void Visualizer::drawVoxel(int voxelsPerDimetion, const MBoundingBox& bb, int i, int j, int k){
	float sizex = bb.width()/voxelsPerDimetion;
	float sizey = bb.height()/voxelsPerDimetion;
	float sizez = bb.depth()/voxelsPerDimetion;

	MString msizex, msizey, msizez;
	msizex.set(sizex);
	msizey.set(sizey);
	msizez.set(sizez);
	MString cmd1("polyCube -ch on -o on -w ^1s -h ^2s -d ^3s -cuv 4 -name pCube^4s^5s^6s");
	MString cmd2("select -r pCube^1s^2s^3s");
	MString cmd3("move -r ^1s ^2s ^3s");
	MString si, sj, sk;
	MString sx, sy, sz;

	si.set(i);
	sj.set(j);
	sk.set(k);
	sx.set(bb.min().x + (i + 0.5)*bb.width()/(double)voxelsPerDimetion);
	sy.set(bb.min().y + (j + 0.5)*bb.height()/(double)voxelsPerDimetion);
	sz.set(bb.min().z + (k + 0.5)*bb.depth()/(double)voxelsPerDimetion);
	MString cmd;
	cmd.format(cmd1, msizex, msizey, msizez, si, sj, sk);
	MGlobal::executeCommand(cmd, true, true);
	cmd.format(cmd2, si, sj, sk);
	MGlobal::executeCommand(cmd , true, true);
	cmd.format(cmd3, sx, sy, sz);
	MGlobal::executeCommand(cmd, true, true);
	MGlobal::executeCommand("select -cl", false, true);
}

void Visualizer::selectVoxel(int i, int j, int k){
	MString cmd1("select -tgl pCube^1s^2s^3s");
	MString si, sj, sk;
	si.set(i);
	sj.set(j);
	sk.set(k);
	MString cmd;
	cmd.format(cmd1, si, sj, sk);
	MGlobal::executeCommand(cmd , true, true);
}

void Visualizer::drawPoint(double x, double y, double z, const char* id){
	MString cmd1("polySphere -ch on -o on  -r 0.1 -name ^1s");
	MString cmd2("select -r ^1s");
	MString cmd3("move -r ^1s ^2s ^3s");
	MString sx, sy, sz;

	MString name;
	MStatus stat = name.set(id);
	sx.set(x);
	sy.set(y);
	sz.set(z);
	MString cmd;
	cmd.format(cmd1, name);
	MGlobal::executeCommand(cmd, true, true);
	cmd.format(cmd2, name);
	MGlobal::executeCommand(cmd , true, true);
	cmd.format(cmd3, sx, sy, sz);
	MGlobal::executeCommand(cmd, true, true);
	MGlobal::executeCommand("select -cl", true, true);
}

void Visualizer::deselectAll()
{
  MGlobal::executeCommand("select -cl", true, true);
}